/*****************************************************************************************
   
   Copyright (c) Microsoft Corporation. All rights reserved.

   Use of this code sample is subject to the terms of the Microsoft
   Permissive License, a copy of which should always be distributed with
   this file.  You can also access a copy of this license agreement at:
   http://www.microsoft.com/resources/sharedsource/licensingbasics/permissivelicense.mspx

 ****************************************************************************************/

using System;

namespace GrooveCommands
{
	class GrooveExportSpaceAsArchive : CommandUtils.GrooveUtilBase
	{
		private string m_SpaceURI;
		private string m_FilePath;
		private bool   m_IncludeMembers;
		private bool   m_NoData;
		private string m_Passphrase;
		private bool   m_ReadOnly;

		[STAThread]
		static void Main(string[] i_Arguments)
		{
			CommandUtils.UtilAppMain.RunApplication(new GrooveExportSpaceAsArchive(i_Arguments));
		}

		public GrooveExportSpaceAsArchive(string[] i_Arguments)
			: base (i_Arguments)
		{
			base.UsageMessage = "GrooveExportSpaceAsArchive [PARAMS...]";
			base.HelpMessage = 
				"GrooveExportSpaceAsArchive\r\n\t" + 
				"Exports the specified Groove space to an archive file (GSA)\r\n" +
				"Parameters:\r\n\t" +
				"/Space=             The Web Services URI for the Groove space\r\n\t" +
				"/Path=              The filesystem path for the GSA to be created\r\n\t" +
				"[/Account=          The Web Services URI for the Groove account]\r\n\t" +
				"[/Identity=         The Web Services URI for the Groove identity]\r\n\t" +
				"[/IncludeMembers    Includes the memberlist in the archive (default does not)]\r\n\t" +
				"[/NoData            Creates an archive with tools but no data]\r\n\t" +
				"[/Passphrase        Passphrase to protect the archive]\r\n\t" +
				"[/ReadOnly          Creates a readonly archive file]\r\n\t" +
				"[/RequestKey=       Token used to communicate Groove host]\r\n\t" +
				"[/Host=             The remote Groove host name to issue commands to]\r\n\t" +
				"[/Port=             The remote Groove host port to issue commands to]\r\n\t" +
				"[/Timeout=          Timeout (in ms) for Web Services method execution]\r\n\t" +
				"[/X=                Configuration file to use for parameter input]\r\n\t" +
				"\r\n";
		}

		public override CheckResult CheckParameters()
		{
			CommandUtils.UtilBase.CheckResult CheckResult = base.CheckParameters();
			if (CheckResult != CommandUtils.UtilBase.CheckResult.Succeeded )
				return CheckResult;
		
			m_SpaceURI = base.GetParameterValue("Space");
			if (System.String.IsNullOrEmpty(m_SpaceURI))
			{
				base.DisplayUsage("You must specify a space.");
				return CommandUtils.UtilBase.CheckResult.Failed;
			}

			m_FilePath = base.GetParameterValue("Path");
			if( System.String.IsNullOrEmpty(m_FilePath))
			{
				base.DisplayUsage("You must specify a path for the archive file.");
				return CommandUtils.UtilBase.CheckResult.Failed;
			}

			m_IncludeMembers = base.GetParameterValueFlag("IncludeMembers");

			m_NoData = base.GetParameterValueFlag("NoData");

			m_Passphrase = base.GetParameterValue("Passphrase");
			if( m_Passphrase==null )
			{
				m_Passphrase = "";
			}

			m_ReadOnly = base.GetParameterValueFlag("ReadOnly");

			return base.CheckUnusedParameters();
		}

		public override CommandUtils.UtilBase.CheckResult Run()
		{
			GrooveWebServicesV12Helpers.Space Space = null;

			try
			{
				Space = base.Identity.OpenSpace(m_SpaceURI);
			}
			catch (System.Exception Exception)
			{
				base.StandardError.WriteLine("Unable to open space: " + Exception.Message);
				throw Exception;
			}

			try
			{
				Space.ExportAsArchive(
					m_FilePath,
					m_IncludeMembers,
					!m_NoData,
					m_Passphrase,
					m_ReadOnly
					);
			}
			catch (System.Exception Exception)
			{
				base.StandardError.WriteLine("Unable to arhive the space: " + Exception.Message);
				throw Exception;
			}

			return CommandUtils.UtilBase.CheckResult.Succeeded;
		}
	}

}
